package com.yiwenup.leetcode.top_interview_150;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-16 22:12:48
 * @Description https://leetcode.cn/problems/4sum-ii/description/
 */
public class No0454 {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer num1 : nums1) {
            for (Integer num2 : nums2) {
                int sum = num1 + num2;
                Integer count = map.get(sum);
                if (count == null) {
                    map.put(sum, 1);
                } else {
                    map.put(sum, ++count);
                }
            }
        }
        for (Integer num3 : nums3) {
            for (Integer num4 : nums4) {
                if (map.get(-(num3 + num4)) != null) {
                    ans += map.get(-(num3 + num4));
                }
            }
        }

        return ans;
    }
}
